import { _decorator, AudioClip, AudioSource, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

interface IAudioMap {
    [name: string]: AudioClip;
}

@ccclass('AudioManager')
export class AudioManager extends Component {

    @property([AudioClip])
    private audioList: AudioClip[] = [];

    private dict: IAudioMap = {};

    private audioSource: AudioSource = null;

    start() {
        for (let index = 0; index < this.audioList.length; index++) {
            const element = this.audioList[index];
            this.dict[element.name] = element;

        }

        this.audioSource = this.getComponent(AudioSource);
    }


    public play(name: string) {
        const audioclip = this.dict[name];
        if (audioclip !== undefined) {
            this.audioSource.playOneShot(audioclip);
        }
    }

    update(deltaTime: number) {

    }
}

